<?php
namespace Home\Controller;
use \Home\Controller\IndexController;
class AdminController extends IndexController 
{
    public function add()
    {
        //传递页值
        $_p = $p | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
    	if(IS_POST)
    	{
            $model = D('Home/Admin');
            if($model->create(I('post.'), 1))
            {
                if($id = $model->add())
                {
                    $this->success('添加成功！', U('lst?p='.$p));
                    exit;
                }
            }
            $this->error($model->getError());
    	}
        //取出所有角色
        $rolemodel = M('Role');
        $roleData = $rolemodel->select();
        $this->assign('roleData',$roleData);

            $this->display();
    }
    public function edit()
    {
        //传递页值
        $_p = $p | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $adminId = session('id');
    	$id = I('get.id');
        if($adminId>1 && $adminId!=$id){
            $this->error('无权修改!');
        }
    	if(IS_POST)
    	{
            $model = D('Home/Admin');
            if($model->create(I('post.'), 2))
            {
                if($model->save() !== FALSE)
                {
                    $this->success('修改成功！', U('lst', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
    	}
    	$model = M('Admin');
    	$data = $model->find($id);
    	$this->assign('data', $data);
        
        $rolemodel = M('Role');
        $roleData = $rolemodel->select();
        $this->assign('roleData',$roleData);
        
        $arolemodel = M('AdminRole');
        $aroleData = $arolemodel->field('group_concat(role_id) role_id')->where("admin_id=$id")->find();
        $rid=$aroleData['role_id'];
        $this->assign('rid',$rid);

        $this->display();
}
    public function delete()
    {
        $adminId = session('id');
    	$id = I('get.id');
        if($adminId>1 && $adminId!=$id){
            $this->error('无权删除!');
        }
    	$model = D('Home/Admin');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
            $this->success('删除成功！', U('lst', array('p' => $p)));
            exit;
    	}
    	else 
    	{
            $this->error($model->getError());
    	}
    }
    public function lst()
    {
        //传递页值
        $_p = $p | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
    	$model = D('Home/Admin');
    	$data = $model->search();
    	$this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
    	));

    	$this->display();
    }
}